<?php
/**
 * User: hjd-yao
 * Date: 2019/2/22
 * Time: 16:22
 */

namespace App\Http\Controllers\Lottery;


use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use Illuminate\Http\Request;
use App\Models\Lottery\PrizeModel;

class PrizeController extends BackendController
{
    private static $_prizeModel;

    public function __construct()
    {
        self::$_prizeModel = PrizeModel::init();
    }

    /**
     * 奖金列表
     */
    public function index()
    {
        $list = self::$_prizeModel->paginate($this->getPerPageNum());
        return view('backend::lottery.prize.list',[
            'page_title'=>'奖品列表',
            'list'=>$list
        ]);
    }

    /**
     * @title 发布公告
     * @param Request $request
     * @return SelfResponse|\Illuminate\View\View
     */
    public function create(Request $request)
    {
        if($request->isMethod('post')){
            self::$_prizeModel->title = $request->post('title');
            self::$_prizeModel->cover = $request->post('cover');
            self::$_prizeModel->power = $request->post('power');
            if(self::$_prizeModel->save()){
                return SelfResponse::success('添加成功！','b_lottery_prize_index');
            }
            return SelfResponse::error('添加失败,请重新尝试');
        }else{
            return view('backend::lottery.prize.create',[
               'page_title'=>'添加奖品',
            ]);
        }
    }

    /**
     * @title 修改公告
     * @param Request $request
     * @param $id
     * @return SelfResponse|\Illuminate\View\View
     */
    public function update(Request $request,$id)
    {
        $info = self::$_prizeModel::find($id);
        if($request->isMethod('post')){
            $info->title = $request->post('title');
            $info->cover = $request->post('cover');
            $info->power = $request->post('power');
            if($info->save()){
                return SelfResponse::success('修改成功！','b_lottery_prize_index');
            }
            return SelfResponse::error('修改失败,请重新尝试');
        }else{
            return view('backend::lottery.prize.update',[
                'page_title'=>'编辑奖品',
                'info'=>$info
            ]);
        }
    }

    /**
     * @title 删除
     */
    public function delete($id)
    {
        $info = self::$_prizeModel->find($id);

        if(!$info){
            return SelfResponse::error('信息不存在！');
        }

        if(!self::$_prizeModel->where('id',$id)->delete()){
            return SelfResponse::error('删除失败！');
        }

        return SelfResponse::success('删除成功！','b_lottery_prize_index');
    }
}